import express from "express"
import dbhelp from "./../../lib/dbhelp"
let adminRouter=express.Router()
module.exports=adminRouter

adminRouter.get("/index",function(request,response,next){
	let menuSql="SELECT *FROM sys_menu WHERE m_status=1 ORDER BY m_orderby"
	let websiteSql="SELECT *FROM websiteinfo "
	dbhelp.query(websiteSql,[],function(err,result1){
		console.log(result1)
		dbhelp.query(menuSql,[],function(err,result2){
			response.render("./admin/index.html",{menu:result2,webinfo:result1})
		})
		
		
	})
	
})
adminRouter.get("/menu",function(request,response,next){
	response.render("./admin/menu.html",{})
})

//向数据库请求数据
adminRouter.post("/getMenu",function(request,response,next){
	let sql="SELECT *FROM sys_menu  ORDER BY m_orderby"
	dbhelp.query(sql,[],function(err,result){
		var obj={
			code: 0,
			msg: "",
			count: result.length,
			data:result
		}
		response.json(obj)
	})

	
})
//软删除数据（修改状态）
adminRouter.post("/deleData",function(request,response,next){
	let sql="UPDATE  sys_menu SET m_status=0 WHERE m_id =?"
	//console.log(request.body)//{ m_id: '2', m_num: '1000' ...}
	dbhelp.query(sql,request.body.mId,function(err,result){
		if(!err&&result.affectedRows>=1){
			response.json({msg:"删除成功",status:1})
		}else{
			response.json({msg:"删除失败",status:-1})
		}
	})
	
})

//编辑数据
adminRouter.post("/updateData",function(request,response,next){
	let sql="UPDATE  sys_menu SET m_name=?,m_url=?,m_num=?,m_parent=?,m_icon=?,m_orderby=? WHERE m_id =?"
	let params=[
		request.body.m_name,
		request.body.m_url,
		request.body.m_num,
		request.body.m_parent,
		request.body.m_icon,
		request.body.m_orderby,
		request.body.m_id,
	]
	//console.log(request.body)//{ m_id: '2', m_num: '1000' ...}
	dbhelp.query(sql,params,function(err,result){
		if(!err&&result.affectedRows>=1){
			response.json({msg:"修改成功",status:1})
		}else{
			response.json({msg:"修改失败",status:-1})
		}
	})
	
})
//修改状态
adminRouter.post("/updateStatus",function(request,response,next){
	let sql="UPDATE  sys_menu SET m_status=? WHERE m_id =?"
	let params=[
		request.body.m_status,
		request.body.mId,
	]
	//console.log(request.body)//{ m_id: '2', m_num: '1000' ...}
	dbhelp.query(sql,params,function(err,result){
		if(!err&&result.affectedRows>=1){
			response.json({msg:"状态修改成功",status:1})
		}else{
			response.json({msg:"状态修改失败",status:-1,err:err})
		}
	})
	
})

adminRouter.get("/addmenu",function(request,response,next){
	response.render("./admin/addmenu.html",{})
})

//添加菜单
adminRouter.post("/addmenu",function(request,response,next){
	let sql="INSERT INTO sys_menu (`m_name`,`m_url`,`m_num`,`m_status`,`m_parent`,`m_icon`,`m_orderby`) VALUES (?,?,?,?,?,?,?)"
	let params=[
		request.body.m_name,
		request.body.m_url,
		request.body.m_num,
		request.body.m_status,
		request.body.m_parent,
		request.body.m_icon,
		request.body.m_orderby,
		request.body.m_id,
	]
	//console.log(request.body)//{ m_id: '2', m_num: '1000' ...}
	dbhelp.query(sql,params,function(err,result){
		if(!err&&result.affectedRows>=1){
			response.json({msg:"添加成功",status:1})
		}else{
			response.json({msg:"添加失败",status:-1,err:err})
		}
	})
	
})